package UiLayer;
import java.util.Scanner;
import ControlLayer.SaleCtr;
import java.util.ArrayList;

/**
 * Write a description of class SaleUI here.
 * 
 * @author (Gruppe 2) 
 * @version (a version number or a date)
 */
public class SaleUI
{    
    private SaleCtr saleCtr;

    public SaleUI()
    {
        saleCtr = new SaleCtr();
        start(saleCtr);
    }

    public void start(SaleCtr ctr)
    {
        saleCtr = ctr;
        saleMenu();
    }

    public void saleMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choice = writeSaleMenu();
            if(choice == 1)
            {
                System.out.println("\f");
                findSale(inputID());
            }
            if(choice == 2)
            {
                System.out.println("\f");
                createCustomerSale(inputID(), inputItemID(), inputCustomerID(), inputRegDate(), inputStatus(), inputQuantity());
            }
            if(choice == 3)
            {
                System.out.println("\f");
                createBusinessSale(inputID(), inputItemID(), inputBusinessID(), inputRegDate(), inputStatus(), inputQuantity());
            }
            if(choice == 4)
            {
            	System.out.println("\f");
            	addItemToSale(inputID(), inputItemID(), inputQuantity());
            }
            if(choice == 5)
            {
                System.out.println("\f");
                deleteSale(inputID());
            }
            if(choice == 6)
            {
                System.out.println("\f");
                printSaleList();
            }
            if(choice == 7)
            {
                exit = true;
                System.out.println("\f");
            }
        }
    }
    
    private int writeSaleMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(""); //This blank line are made so that everything wont be printed right on top of each other
        System.out.println("*** Sale Menu ***");
        System.out.println(" (1) Show sale information");
        System.out.println(" (2) Create sale for customer");
        System.out.println(" (3) Create sale for business customer");
        System.out.println(" (4) Add another item to a sale");
        System.out.println(" (5) Delete sale");
        System.out.println(" (6) Show sales list");
        System.out.println(" (7) Return");
        System.out.print("\n Make your choice: ");
        
        boolean done = false;
        int choice = 0;
        while(!done)
        {
            try
            {
                choice = keyboard.nextInt();
                done = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }
        return choice;
    }

    private int inputID()
    {
        boolean done = false;
        int id = 0;
        while(!done)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Type Sale ID: ");
            try
            {
                id = keyboard.nextInt();
                done = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }
        return id;
    }

    private int inputItemID()
    {
        boolean done = false;
        int itemId = 0;
        while(!done)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Type Item ID: ");
            try
            {
                itemId = keyboard.nextInt();
                done = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }
        return itemId;
    }

    private int inputCustomerID()
    {
        boolean done = false;
        int customerId = 0;
        while(!done)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Type Customer ID: ");
            try
            {
                customerId = keyboard.nextInt();
                done = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }
        return customerId;
    }
    
    private int inputBusinessID()
    {
        boolean done = false;
        int businessId = 0;
        while(!done)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Type Business customer ID: ");
            try
            {
                businessId = keyboard.nextInt();
                done = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }
        return businessId;
    }

    private String inputRegDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type registration date: ");
        String regDate = keyboard.nextLine();
        return regDate;
    }
    
    private String inputStatus()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type status of the sale: ");
        String status = keyboard.nextLine();
        return status;
    }

    private int inputQuantity()
    {
        boolean done = false;
        int quantity = 0;
        while(!done)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Type quantity: ");
            try
            {
                quantity = keyboard.nextInt();
                done = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }
        return quantity;
    }
    
    private void findSale(int id)
    {
        saleCtr.printSale(id);
    }
    
    private void createCustomerSale(int id, int itemId, int customerId, String regDate, String status, int quantity)
    {
        saleCtr.createCustomerSale(id, itemId, customerId, regDate, status, quantity);
    }
    
    private void createBusinessSale(int id, int itemId, int businessId, String regDate, String status, int quantity)
    {
        saleCtr.createBusinessSale(id, itemId, businessId, regDate, status, quantity);
    }
    
    private void addItemToSale(int id, int itemId, int quantity)
    {
    	saleCtr.addItemToSale(id, itemId, quantity);
    }
    
    private void deleteSale(int id)
    {
        saleCtr.removeSale(id);
    }
    
    private void printSaleList()
    {
        saleCtr.printSaleList();
    }
}
